using System.Collections.Generic;
using CJ.XNFZ;
using CJ.XNFZ.SCORE;
using CJ.XNFZ.TEST;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
public class Test : MonoBehaviour {

    public TestsystemHelper[] helpers;

    public GameObject testPanel;
    public Text title;
    public List<Toggle> options;
    public List<Text> optionTexts;
    public List<GameObject> rightMarks;
    public List<GameObject> wrongMarks;
    public GameObject okButton;
    public GameObject nextButton;

    public GameObject finishPanel;
    public Text showRightCountText;
    public Text showCountText;
    public Text showScoreText;

    public List<Question> questionList = new List<Question> ();
    public List<string> questionIDList = new List<string> ();
    public List<string> questionLogList = new List<string> ();
    int currentQuestion;
    int currentTest;

    public void SetupTest (int index) {
        if (helpers.Length == 0) return;
        currentTest = index;
        if (index < 0) currentTest = 0;
        if (index >= helpers.Length) currentTest = helpers.Length - 1;
        testPanel.SetActive (true);
        finishPanel.SetActive (false);
        currentQuestion = 0;
        this.questionList.Clear ();
        this.questionList.AddRange (helpers[currentTest].config.questions);
        questionIDList.Clear ();
        questionLogList.Clear ();
        ResetUI ();
        SetUI (questionList[0]);
        TimeHelper.Instance.SetStart ();
    }

    public void OnOK () {
        questionIDList.Add (questionList[currentQuestion].id);
        if (options[questionList[currentQuestion].correct].isOn) {
            questionLogList.Add ("正确");

            rightMarks[questionList[currentQuestion].correct].SetActive (true);
        } else {
            questionLogList.Add ("错误");

            rightMarks[questionList[currentQuestion].correct].SetActive (true);
            for (int i = 0; i < options.Count; i++) {
                if (options[i].isOn) {
                    wrongMarks[i].SetActive (true);
                }
            }
        }

        for (int i = 0; i < options.Count; i++) {
            options[i].interactable = false;
        }

        okButton.SetActive (false);
        nextButton.SetActive (true);
    }

    public void OnNext () {
        currentQuestion++;
        if (currentQuestion >= questionList.Count) {
            ShowFinish ();
            return;
        }
        ResetUI ();
        SetUI (questionList[currentQuestion]);
        okButton.SetActive (true);
        nextButton.SetActive (false);
    }

    void ResetUI () {
        okButton.SetActive (true);
        nextButton.SetActive (false);

        for (int i = 0; i < options.Count; i++) {
            options[i].interactable = true;
        }
        for (int i = 0; i < options.Count; i++) {
            options[i].gameObject.SetActive (false);
            options[i].isOn = false;
            rightMarks[i].gameObject.SetActive (false);
            wrongMarks[i].gameObject.SetActive (false);
        }
    }

    void SetUI (Question q) {
        title.text = q.stem;

        //Shuffle (q.alternatives);
        for (int i = 0; i < q.alternatives.Length; i++) {
            options[i].gameObject.SetActive (true);
            optionTexts[i].text = q.alternatives[i];
        }
    }

    void ShowFinish () {

        TimeHelper.Instance.SetStop ();
        testPanel.SetActive (false);
        finishPanel.SetActive (true);
        int rrr = 0;
        int wrrr = 0;
        float sss = 0;
        foreach (var a in questionLogList) {
            if (a == "正确") {
                rrr += 1;
            }
            if (a == "错误") {
                wrrr += 1;
            }
        }
        sss = (Mathf.Floor (100 * (rrr / (float) questionLogList.Count)));
        showRightCountText.text = rrr.ToString ();
        showCountText.text = questionLogList.Count.ToString ();
        showScoreText.text = sss.ToString ();

        questionIDList.Add ("score");
        questionLogList.Add (sss.ToString ());
        ScoreSystemHelper.Instance.UploadScore (helpers[currentTest].config.name, helpers[currentTest].config.id, helpers[currentTest].config.version, (int) sss, TimeHelper.Instance.startTime, TimeHelper.Instance.endTime, TimeHelper.Instance.usedTime, questionIDList, questionLogList);

    }

    static List<T> Shuffle<T> (List<T> list) {
        for (int i = 0; i < list.Count; i++) {
            int k = UnityEngine.Random.Range (0, i);
            T value = list[k];
            list[k] = list[i];
            list[i] = value;
        }
        return list;
    }

}